<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use DB;
use Cookie;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AgentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        return view('admin.agent.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getShow(Request $request)
    {
        $id = $request->input('agent_id','');
        $agent_id = Cookie::get('logon')['agent_id'];
        $data = DB::table('agent')
            -> where(function($query) use ($id,$agent_id) {
                if($id){
                    $query->where(DB::raw("concat(',', path,',')"), 'like', '%,'.$agent_id.',%')
                        ->where('id',$id);
                } else {
                    $query->where(DB::raw("concat(',', path,',')"), 'like', '%,'.$agent_id.',%');
                }
            })
            -> get();
        if($data){
            return $data;
        } else {
            return '';
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request->input('id');
        $agent = DB::table('agent') -> where('id',$id)->first();
        return view('admin.agent.edit',['agent'=>$agent]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $data = $request->except('_token');
        $raw = DB::table('agent') -> where('id',$data['id']) -> update(['name'=>$data['name']]);
        if ($raw) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
